﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Util;

namespace ZongHeZhiFa2._0_Centre
{
    public partial class Auth : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string backurl = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
            string domain = JobRequest.GetRequestString("domain");
            WxInfo wi = Common.GetWx();
            if (Request["code"] == null)
            {
                Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wi.AppID + "&redirect_uri=" + Server.UrlEncode(Request.Url.AbsoluteUri) + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
            }
            else
            {
                WeiXin wx = new WeiXin();
                string result = wx.HttpGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wi.AppID + "&secret=" + wi.Secret + "&code=" + Request["code"] + "&grant_type=authorization_code", "");
                string str = wx.HttpGet("https://api.weixin.qq.com/sns/userinfo?access_token=" + wx.ConvertJson(result).access_token + "&openid=" + wx.ConvertJson(result).openid + "&lang=zh_CN", "");
                dynamic user = wx.ConvertJson(str);

                //判断OpenID是否存在
                IDictionary<string, string> resultMap = new Dictionary<string, string>();
                string Act = "Exist_OpenID";
                string Timestamp = Common.GetTimestamp().ToString();
                resultMap.Add("Act", Act);
                resultMap.Add("Timestamp", Timestamp);
                resultMap.Add("Sign", Common.GetSign(Act, Timestamp));
                resultMap.Add("OpenID", user.openid);
                string param = Common.joinObjectMapValue(resultMap, '&');
                string json = wx.HttpPost(domain + "/tools/member.ashx", param);
                Common.Log("log1:"+json);
                dynamic dy = wx.ConvertJson(json);
                if (dy.State)
                {
                    if (DataConvert.ToString(dy.Data) == "")
                    {
                        //注册平台用户账号
                        resultMap = new Dictionary<string, string>();
                        Act = "Regist_WxUser";
                        Timestamp = Common.GetTimestamp().ToString();
                        resultMap.Add("Act", Act);
                        resultMap.Add("Timestamp", Timestamp);
                        resultMap.Add("Sign", Common.GetSign(Act, Timestamp));
                        resultMap.Add("OpenID", user.openid);
                        resultMap.Add("NickName", user.nickname);
                        resultMap.Add("HeadImg", user.headimgurl);
                        param = Common.joinObjectMapValue(resultMap, '&');
                        json = wx.HttpPost(domain + "/tools/member.ashx", param);
                        dy = wx.ConvertJson(json);
                        Common.Log("log2:" + json);
                    }
                    Response.Redirect(domain + "/h5/#/pages/h5/home?Token=" + dy.Data);
                }else
                {
                    Response.Write(dy.Msg);
                }
            }
        }
    }
}